const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");
let fs = require("fs")
const CLIPDATA_TYPE = {
    max:{
        fixedhp: 298.75,
        fixedAtt: 19.45,
        fixdefence: 23.15,
        reaction : 23.31,
        attRate: 5.83, 
        hpRate: 5.83, 
        criticalRate: 3.89,
        criticalStrike: 7.77,
        defRate: 7.29,
        charging: 6.48
    },
    mid:{
        fixedhp: 253.94,   // 209.13  239   268.88   298.75
        fixdefence: 19.675, // 16.2   18.52  20.83  23.15
        fixedAtt: 16.535,   //  13.62   15.56   17.51  19.45
        reaction : 19.815,  // 16.32   18.65  20.98  23.31
        attRate: 4.955,  // 4.08  4.66  5.25  5.83
        hpRate: 4.955,   // 4.08  4.66  5.25  5.83
        criticalRate: 3.305,   //  2.72  3.11   3.5   3.89
        criticalStrike: 6.605,  //  5.44  6.22  6.99  7.77
        defRate: 6.195,   //  5.1  5.83  6.56   7.29
        charging: 5.505  //  4.53   5.18   5.83   6.48
    }
}


const CLIPDATA = CLIPDATA_TYPE.mid

const CONFIG = {
    chance: 20,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        criticalRate: 31.1,
        // attRate: 46.6,
        dmgRate: 46.6,
        attRate: 46.6,
        // reaction: 187,
        // charging: 51.8
    },
    clips: ["charging", "attRate", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["charging", "attRate", "criticalRate", "criticalStrike"], // 花
        ["charging", "attRate", "criticalRate", "criticalStrike"], // 毛
        ["reaction", "charging", "criticalRate", "criticalStrike"], // 沙
        ["charging", "attRate", "criticalRate", "criticalStrike"], // 杯
        ["reaction", "charging", "attRate", "criticalStrike"] // 头
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        { charging:20 },
        // { fixedAtt: 606,  criticalStrike:60 }, // 九条
        { dmgRate: 40, attRate: 20, jk:40}, // 万叶
        // { reaction: 240}, // 草神
        {criticalStrike : 60},
        // { attRate: 48, dmgRate: 60, criticalRate: 15}, // 莫娜
        { fixedAtt: 1203,attRate: 20} // 班尼
    ]
}

let charaters = {
    units : {
        name: "九条",    // 10级e  瞬间释放对周围造成210.96%雷伤 之后的攻击协同造成75.6%雷伤 联机的时候队友的协同攻击伤害为原本的20% 协同攻击最快0.9s一次  大招增伤元素能量*0.3
        reaction: 0,    // 10级q  90能量 18s 瞬间释放721.44%雷伤 之后进行的普攻连段都视为大招伤害 并为队友充能 1s/次 一共可以充能五次 每次回能量2.5
        charging: 100,   // q后普攻连段(持续7s) 79.82% 78.42% 96.02%  55.11% 55.26% 131.92%    重击 109.9% + 132.67% 
        attRate: 24,     // 队友的大招会为雷神积攒愿力(最高60层) 0.2愿力/1能量  0.87%攻击力/1愿力   大招增加7%伤害/1愿力  例如80能量大招给积攒16愿力     持续到脱离战斗后300s消除愿力
        defence: 628,
        hp:9570,
        base: 195.41,         
        criticalRate: 5,    // 被动 充能超过100%的部分  提供0.6大招回能/每1%   雷伤加成 0.4% / 1%
        criticalStrike: 50,
        weapontype : "bow", 
        skills: {
            a: [
                function(data){},
            ]
        }
    }
}

calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        if(weapon.weapontype == charaters.units.weapontype) {
            charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
                count++;
                data = objValueAdd([data, ...CONFIG.buffs]);
                data.hp = data.hp*(1 + (data.hpRate||0)/100) + 4780;

                // 0.9129
                let atkAdd = 0.9129 * data.base
                data.fixedAtt += atkAdd;

                if(weapon.skill) { weapon.skill(data); }
                
                let slipQadd = data.charging * 0.25
                if(slipQadd > 75) {
                    slipQadd = 75
                }
                
                // 8.704 0.725 * 6

                // e 大招增伤 + 27%
                let qdmgRate = 0.3 * 80;

                // 被动雷伤

                let Q1Dmg = getDmg({
                    data: {
                        ...data,
                        dmgRate: data.dmgRate + qdmgRate + slipQadd + (data.qDmgAdd||0)
                    },
                    rate:   8.704
                }).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //

                let Q2Dmg = getDmg({
                    data: {
                        ...data,
                        dmgRate: data.dmgRate + qdmgRate + slipQadd + (data.qDmgAdd||0)
                    },
                    rate:   0.725
                }).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //

                if(data.phyRate) { // 物理特效伤害
                    data.phyDmg = getDmg({data, rate:data.phyRate}).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
                }
                let point = Q1Dmg + Q2Dmg * 6  
                let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
                return { point, data, second, attack, dmg: { 
                    Q1Dmg, Q2Dmg,atkAdd
                } }
            }))
        }
    })
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point)
    });

    let sortf = JSON.parse(fs.readFileSync("./sort.js").toString()) 
    let choose = charactersDmg[4];
    console.log(choose);
    let name = choose.data.name
    let keys = "_"+choose.data.weaponname+"_3命无拐毕业"
    let sortfData = {key: name+keys, point:choose.point, dmg:JSON.parse(JSON.stringify(choose.dmg))}
    if(sortfData.point && sortfData.point > 0) {
        let org = sortf.findIndex(item3 => item3.key == sortfData.key)
        if(org >= 0){
            sortf[org] = sortfData
        } else {
            sortf.push(sortfData)
        }
        sortf.sort((b,a) => a.point-b.point)
        // fs.writeFileSync("./sort.js", JSON.stringify(sortf))
    }

    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                })
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second);
    })
    _ret.sort((b,a)=> a.point-b.point);
    return _ret[0];
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
